import { getMailList, getMailDelete } from '@/service/modules/mail'
import { IMailList } from '@/types';
import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';

export interface MailModelState {
    mailList: IMailList[],
    MailDelete: object,
}


export interface MailModelType {
    namespace: 'mail';
    state: MailModelState;
    effects: {
        getMailList: Effect;
        getMailDelete: Effect,
    };
    reducers: {
        save: Reducer<MailModelState>;
    };
}

const MailModel: MailModelType = {
    // 数据模型，用来隔离数据防止相互影响
    namespace: 'mail',

    // 数据模型状态，相当于vuex的state
    state: {
        mailList: [],
        MailDelete: {},
    },

    // 异步操作，相当于vuex的action
    effects: {
        // 先简单记忆，*相当于async，yield相当于await
        *getMailList({ payload }, { call, put }) {
            let result = yield call(getMailList, payload);
            console.log(result, "----------------------");

            if (result.data) {
                yield put({
                    type: 'save',
                    payload: {
                        mailList: result.data[0],
                        mailLength: result.data[1]
                    }
                })
            }
        },
         //   删除
         *getMailDelete({ payload }, { call, put }) {
            const result = yield call(getMailDelete, payload)
            console.log(payload);
            if (result.data) {
                yield put({
                    type: "save",
                    payload: {
                        MailDelete: result
                    }
                })
            }
        },
    },

    // 同步操作，相当于vuex的mutation
    reducers: {
        save(state, action) {
            return {
                ...state,
                ...action.payload,
            };
        },
    },
};

export default MailModel;
